﻿
@{
    ViewBag.Title = "ContractPay";
    Layout = "~/Views/Shared/_Layout_iframe.cshtml";
}

<h2>合同支付</h2>
<div style="padding: 16px">

    <div class="layui-card" style="height: 800px;">
        <div class="layui-card-body" style="height: max-content;">
         
            <table class="layui-hide" id="test" lay-filter="test"></table>
        </div>


        <form class="layui-form" lay-filter="packageform" action="" style="margin-left: 100px;margin-right: 100px;">
            <input type="hidden" id="hfid" name="id" value="@ViewBag.Id" />
            <div class="layui-form-item" style="display: flex;flex-wrap: wrap;">
                <label class="layui-form-label">合同状态</label>
                <div class="layui-input-inline">
                    <input type="tel" name="SignState" lay-verify="required" autocomplete="off" class="layui-input" style="height: 30px;" disabled>
                </div>
            </div>
            <div class="layui-form-item" style="display: flex;flex-wrap: wrap;">
                <label class="layui-form-label">产妇名称</label>
                <div class="layui-input-inline">
                    <input type="tel" name="ParturientName" lay-verify="required" autocomplete="off" class="layui-input" style="height: 30px;" disabled>
                </div>
            </div>
            <div class="layui-form-item" style="display: flex;flex-wrap: wrap;">
                <label class="layui-form-label">合同编号</label>
                <div class="layui-input-inline">
                    <input type="tel" name="ContractNumber" lay-verify="required" autocomplete="off" class="layui-input" style="height: 30px;" disabled>
                </div>
            </div>
            <div class="layui-form-item" style="display: flex;flex-wrap: wrap;">
                <label class="layui-form-label">套餐</label>
                <div class="layui-input-inline">
                    <input type="tel" name="PackageName" lay-verify="required" autocomplete="off" class="layui-input" style="height: 30px;"disabled>
                </div>
            </div>
            <div class="layui-form-item" style="display: flex;flex-wrap: wrap;">
                <label class="layui-form-label">套餐价格</label>
                <div class="layui-input-inline">
                    <input type="tel" name="PackagePrice" lay-verify="required" autocomplete="off" class="layui-input" style="height: 30px;"disabled>
                </div>
            </div>
            <div class="layui-form-item" style="display: flex;flex-wrap: wrap;">
                <label class="layui-form-label">押金</label>
                <div class="layui-input-inline">
                    <input type="tel" name="Deposit" lay-verify="required" autocomplete="off" class="layui-input" style="height: 30px;">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button type="submit" class="layui-btn" lay-submit="" lay-filter="demo1">支付宝付款</button>
                    <button type="submit" class="layui-btn" lay-submit="" lay-filter="demo2">现金付款</button>
                </div>
            </div>

        </form>
    </div>
</div>
<script>
    function initContract() {
        let Id = $("#hfid").val()
        $.post("/Contract/GetPayDetail/" + Id, function (resp) {
            if (resp.code == 0) {
                let contractStatus = resp.data.SignState;
  
                let statusText;
                if (contractStatus = 0) {
                    statusText = "未签约"
                } else {
                    statusText = "已签约"
                }
                form.val('packageform', resp.data)
                $("input[name='SignState']").val(statusText); 
            }

        })
    }
    //Demo
    var form
    layui.use('form', function () {
        initContract();
  form = layui.form;
   form.render('select');
        form.on('submit(demo1)', function(data){
            // 获取表单数据
            var formData = data.field;
            // 跳转到支付页面，并传递数据
            var targetUrl = "/Contract/PayPage"; // 新页面的URL
            var queryString = $.param(formData); 
            // 拼接URL并跳转
            var fullUrl = targetUrl + "?" + queryString;
            // 打开新选项卡并跳转到支付页面
            var newWindow = window.open(fullUrl, '_blank'); // '_blank'表示在新选项卡中打开
            // 监听新选项卡关闭时关闭当前弹窗
            var checkWindowClosed = setInterval(function () {
                if (newWindow.closed) {
                    clearInterval(checkWindowClosed); // 清除定时器
                    parent.table.reload('testContract');
                    var index = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(index);
                }
            }, 500); // 每500ms检查一次新选项卡是否关闭
            return false;
        });
        form.on('submit(demo2)', function (data) {
            // 获取表单数据
            var formData = data.field;
            // 使用AJAX发送请求
            $.ajax({
                url: '/Contract/CashPay',  // 替换为你自己的接口地址
                type: 'POST',
                data: formData,  // 将表单数据作为请求数据
                success: function (response) {
                    // 请求成功后的处理
                    layer.msg("支付成功！", { icon: 1 });
                    parent.table.reload('testContract');
                    var index = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(index);

                },
                error: function (error) {
                    // 请求失败后的处理
                    
                    layer.msg("支付失败，请稍后重试！", { icon: 2 });
                }
            });

            // 阻止表单默认提交行为
            return false;
            var index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(index);
            return false;
        });
});
</script>

